package task

import event.ServerConfigGoodsTypeChangedEvent
import manager.HttpRequestManager
import manager.ReplenishManager
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

/**
 * 更新货道数据
 */
class UpdateGoodsTypeInfoTask : Runnable {
    override fun run() {
        try {
            ReplenishManager.GoodsTypeInfo.clear()
            val ret = HttpRequestManager.refreshGoodsTypeInfo()
            val json = JSONObject(ret)
            val arr = json.getJSONArray("recarsDeploy")
            val length = arr.length()
            ReplenishManager.GoodsTypeInfo.clear()
            for (i in 0 until length) {
                ReplenishManager.GoodsTypeInfo.add(0)
            }
            for (i in 0 until length) {
                val obj = arr.getJSONObject(i)
                val row = obj.getString("recars").toInt()
                val cols = obj.getString("number").toInt()
                ReplenishManager.GoodsTypeInfo[row - 1] = cols
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        EventBus.getDefault().post(ServerConfigGoodsTypeChangedEvent())
    }
}